Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.3] Add partition collection method #16627

Merged
merged 4 commits into from
Dec 3, 2016
Merged

[5.3] Add partition collection method #16627

merged 4 commits into from
Dec 3, 2016

Conversation

freekmurze
Copy link
Contributor

@freekmurze freekmurze commented Dec 2, 2016

The partition method returns a collection with two elements. Items in the first element did pass the given $callback, items in the second element did not.

Here's an example:

collect(range(1, 10))->partition(function ($i) {
    return $i <= 5;
});

// returns a new collection
// first element is a collection with items 1, 2 ,3, 4, 5
// second element is a collection with items 5, 6, 7, 8, 9, 10

@freekmurze freekmurze changed the title Add partition collection method [5.3] Add partition collection method Dec 2, 2016
$partitions[! (int) $callback($item)][] = $item;
}

return new static($partitions);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't you just return the plain array so you can separate them using lists?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, changed it to output a plain array.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, list works on collections, so I think we should definitely return a collection.

Really no collection method should ever return a plain array. You don't ever want to break the chain.

@GrahamCampbell
Copy link
Member

Looks cool. It should be noted that the strict definition of a partition requires it to be non-empty, though, I guess it's probably ok in practical terms.

@@ -726,6 +726,24 @@ public function forPage($page, $perPage)
}

/**
* Returns an array with two elements. Items in the first element did pass
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please format like the other phpdoc, then looks good. 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm afraid I don't see the formatting problem. Could you tell what's wrong with the current formatting?

Copy link
Member

@GrahamCampbell GrahamCampbell Dec 2, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Short descriptions must be "sort". Exactly one sentence and one line. The rest must be separated.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, thanks for clarifying

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants